package org.lizar.controller.loader;

import java.awt.image.BufferedImage;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.lizar.Web;
import org.lizar.config.i18.I18Msg;
import org.lizar.config.i18.I18Resource;
import org.lizar.log.Log;
import org.lizar.log.Logger;
import org.lizar.util.StringHelper;

@SuppressWarnings("unchecked")
public class BasicLoader {
	public Log log=Logger.newInstance(this.getClass());
	public BasicLoader(String queryPath,HttpServletRequest request,HttpServletResponse response){
		this.request=request;
		this.response=response;
		this.queryPath=queryPath;
		Cookie[] co=request.getCookies();
		if(co!=null){
			cookies=new HashMap<String,Cookie>();
			for(Cookie c:co){
				cookies.put(c.getName(), c);
			}
		}
		String[] ps=queryPath.split("/");
		this.path=new ArrayList<String>(ps.length);
		for(String s:ps){
			if(StringHelper.isNotNull(s)){
				if(s.lastIndexOf(".")!=-1){
					path.add(s.substring(0, s.lastIndexOf(".")));
				}else path.add(s);
			}
		}
	}
	
	public int queryPathInt(int i){
		String path=queryPath(i);
		if(StringHelper.isNull(path)||!StringHelper.isInteger(path)){
			this.setAttr("wx_project_id", 12);
			return 12;
		}
		this.setAttr("wx_project_id", path);
		return Integer.parseInt(path);
	}
	
	
	public String getHeader(String name){
		return request.getHeader(name);
	}
	
	public Map<String,String> getHeaders(){
		Map<String,String> map=new LinkedHashMap<String,String>();
		Enumeration headers=request.getHeaderNames();
		Object o=null;
		for(;headers.hasMoreElements();){
			o=headers.nextElement();
			map.put(o.toString(), request.getHeader(o.toString()));
		}
		return map;
	}
	
	public static ServletContext context;
	
	private Map<String,Cookie> cookies;
	
	private Map<String,Object> modified_params_map;
	
	protected HttpServletRequest request;
	
	protected HttpServletResponse response;
	
	public boolean need_after=true;
	
	private  String queryPath;
	
	private List<String> path;
	
	/**
	 * 
	 * get  language
	 * 
	 * */
	public String lan;
	
	
	public I18Msg I18Msg(){
		return Web.i18;
	}
	
	public HttpSession getSession(){
		return this.request.getSession();
	}
	
	public  String msg(String key){
		
		return Web.i18.get(lan, key);
	}
	
	 void updateParameter(String key,Object value){
		if(modified_params_map==null){
			modified_params_map=new HashMap<String,Object>();
			modified_params_map.putAll(request.getParameterMap());
		}
		modified_params_map.put(key, value);
	}
	
	public  String msg(String key,List<String> args){
		return msg(key,lan,args);
	}
	
	public   String msg(String key,String lan){
		return Web.i18.get(lan, key);
	}
	
	public void setLocale(String lan){
		I18Resource res=Web.i18.get(lan);
		if(res==null){
			if(Web.i18._default!=null){
				log.warn("Locale "+lan+" is not found, system will use the default locale "+Web.i18._default.file.getPath());
				res=Web.i18._default;
				request.setAttribute(Web.i18.var, res.map);
			}else log.warn("Locale "+lan+" is not found, and  default locale is also not found.");
		}else request.setAttribute(Web.i18.var, res.map);
	}
	
	public  String msg(String key,String lan,List<String> args){
		StringBuilder tpl=new StringBuilder(Web.i18.get(lan, key));
		int i=0;
		int start=0;
		int end=0;
		if(args!=null){
			for(;i<args.size();i++){
				start=tpl.indexOf("${");
				if(start==-1)break;
				end=tpl.indexOf("}");
				tpl.replace(start, end+1, args.get(i).toString());
			}
		}
		return tpl.toString();
	}
	
	
	private  String postfix;
	
	public String postfix(){
		return postfix;
	}
	
	public void postfix(String v){
		this.postfix=v;
	}
	
	/**
	 * just for image redirect
	 */
	public BufferedImage image;

	/**
	 * just for static redirect and image redirect
	 */
	public boolean file_cache=false;

	public static final int EVENT_RESPONSE=0;
	
	public static final int EVENT_REQUEST=1;
	
	public static final int EVENT_JSON=2;
	
	public static final int EVENT_XML=6;
	
	public static final int EVENT_JSONP=3;
	
	public static final int EVENT_WRITE_IMG=4;
	
	public static final int EVENT_FILE=5;
	
	
	/**
	 * 
	 * get customer's client ip
	 * 
	 * */
	public String getClientIP() {
	       String ip = request.getHeader("x-forwarded-for");
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	           ip = request.getHeader("Proxy-Client-IP");
	       }
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	           ip = request.getHeader("WL-Proxy-Client-IP");
	       }
	       if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	           ip = request.getRemoteAddr();
	       }
	       return ip; 
	}
	
	/**
	 * 
	 * request path
	 * 
	 * */
	public String getRequestPath(){
		return queryPath;
	}
	
	/**
	 * 
	 * request the path with split by /
	 * 
	 * like if request path is /user/setting/change
	 * 
	 * queryPath(0) will be user
	 * 
	 * queryPath(1) will be setting
	 * 
	 * queryPath(2) will be change
	 * 
	 * queryPath(3) will be ""
	 * 
	 * default will be "" string not null
	 * 
	 * @param index, the request path split into array[],i to locate it
	 * 
	 * @return this method will never get exception, as if i is out of bound, then "" will return.
	 * 
	 * */
	public String queryPath(int i){
		if(i>-1&&i<path.size())return path.get(i);
		return "";
	}
	

	/**
	 * 
	 * 检查第i段querypath的值是等于part
	 * 
	 * */
	public boolean queryPathCheck(int i,String part){
		String result=queryPath(i);
		return StringHelper.equals(result, part);
	}
	
	/**
	 * 
	 * get the web physical path
	 * 
	 * */
	public String getRealPath(){
		return context.getRealPath("");
	}
	
	/**
	 * 
	 * get the web physical path with a sub_path
	 * 
	 * */
	public String getRealPath(String sub_path){
		return context.getRealPath(sub_path);
	}
	
	/**
	 * 
	 * get the web context name
	 * 
	 * */
	public String getContextName(){
		return request.getContextPath();
	}
	
	
	public static final String JPEG="JPEG";
	
	public Cookie getCookie(String name){
		if(cookies==null)return null;
		return cookies.get(name);
	}
	
	public void setCookie(Cookie cookie){
		response.addCookie(cookie);
	}
	
	public void setAttr(String key,Object v){
		request.setAttribute(key, v);
	}
	
	public void setAttrs(Map<String,Object> params){
		for(Entry<String,Object> p:params.entrySet()){
			request.setAttribute(p.getKey(),p.getValue());
		}
	}
	
	public Object getAttr(String key){
		return request.getAttribute(key);
	}
	
	public <T> T getAttr(String key,T _default){
		Object v=request.getAttribute(key);
		if(v==null)return _default;
		else return (T)v;
	}
	
	public void removeAttr(String key){
		request.removeAttribute(key);
	}
	
	public void setSessionAttr(String key,Object v){
		request.getSession().setAttribute(key, v);
	}
	
	public Object getSessionAttr(String key){
		return request.getSession().getAttribute(key);
	}
	
	public void removeSessionAttr(String key){
		request.getSession().removeAttribute(key);
	}
	
	public <T> T getSessionAttr(String key,T _default){
		Object v=request.getSession().getAttribute(key);
		if(v==null)return _default;
		else return (T)v;
	}
	
	public <T> T getSessionAttr(String key,Class<T>  t){
		Object v=request.getSession().getAttribute(key);
		if(v==null)return null;
		else return (T)v;
	}
	
	public void setAppAttr(String key,Object v){
		context.setAttribute(key, v);
	}
	
	public Object getAppAttr(String key){
		return context.getAttribute(key);
	}
	
	public void removeAppAttr(String key){
		context.removeAttribute(key);
	}
	
	public <T> T getAppAttr(String key,T _default){
		Object v=context.getAttribute(key);
		if(v==null)return _default;
		else return (T)v;
	}

	
	/**
	 * 
	 * @return
	 * 重新设置 encode type
	 * 本方法需要在所有获取表单参数之前使用，否者无效
	 * 
	 * **/
	public void encodeParams(String encode_type) throws UnsupportedEncodingException{
		request.setCharacterEncoding(encode_type);
		response.setContentType("text/html;charset="+encode_type);
	}
	
	public String encodeType(){
		return request.getCharacterEncoding();
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 
	 * */
	public int getInt(String key){
		Object v=request.getParameter(key);
		int vr=0;
		if(v==null){
			return 0;
		}
		try{
			vr=Integer.parseInt(v.toString());
		}catch(Exception e){
			return 0;
		}	
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 同时会将返回结果的int类型重新set到request parameters中去
	 * 
	 * */
	public int getInt(String key,int defv){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return defv;
		}
		int vr=0;
		try{
			vr=Integer.parseInt(v.toString());
		}catch(Exception e){
			return defv;
		}	
		return vr;	
	}
	
	/**
	 * 
	 * request.getParameter("parameter_name")
	 * 
	 * if result is null , will return default value ""
	 * 
	 * */
	public String getStr(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return "";
		}
		else {
			String result=v.toString().trim();
			return result;
		}
	}
	
	public String getStr(String key,String defv){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return defv;
		}
		else {
			String result=v.toString().trim();
			return result;
		}
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 
	 * */
	public boolean getBool(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null)return false;
		boolean vr=false;
		try{
			vr=Boolean.parseBoolean(v.toString().trim());
		}catch(Exception e){
			return false;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 
	 * */	 
	public boolean getBool(String key,boolean _default){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return _default;
		}
		boolean vr=false;
		try {
			vr= Boolean.parseBoolean(v.toString().trim());
		}catch(Exception e){
			return _default;
		}
		return vr;
	}
	


	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 
	 * */
	public long getLong(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return 0;
		}
		long vr=0;
		try{
			vr=Long.parseLong(v.toString());
		}catch(Exception e){
			return 0;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 
	 * */
	public long getLong(String key,long _default){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return _default;
		}
		long vr=0;
		try{
			vr=Long.parseLong(v.toString());
		}catch(Exception e){
			return _default;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 
	 * */
	public double getDouble(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return 0;
		}
		double vr=0;
		try{
			vr=Double.parseDouble(v.toString());
		}catch(Exception e){
			return 0;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 
	 * */
	public double getDouble(String key,double _default){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return _default;
		}
		double vr=0;
		try{
			vr=Double.parseDouble(v.toString());
		}catch(Exception e){
			return _default;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 
	 * */
	public float getFloat(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return 0;
		}
		float vr=0;
		try{
			vr=Float.parseFloat(v.toString());
		}catch(Exception e){
			return 0;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 
	 * */
	public float getFloat(String key,float _default){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			return _default;
		}
		float vr=0;
		try{
			vr=Float.parseFloat(v.toString());
		}catch(Exception e){
			return _default;
		}
		return vr;
	}
	

	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 同时会将返回结果的int类型重新set到request parameters中去
	 * 好处：可以直接将parameter的Map数据进行验证后，做持久化工作，以简化数据库操作
	 * 
	 * */
	public int getAndSetInt(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		int vr=0;
		if(v==null){
			updateParameter(key,vr);
			return 0;
		}
		try{
			vr=Integer.parseInt(v.toString());
			updateParameter(key,vr);
		}catch(Exception e){
			return 0;
		}	
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 同时会将返回结果的int类型重新set到request parameters中去
	 * 
	 * */
	public int getAndSetInt(String key,int defv){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key,defv);
			return defv;
		}
		int vr=0;
		try{
			vr=Integer.parseInt(v.toString());
			updateParameter(key,vr);
		}catch(Exception e){
			return defv;
		}	
		return vr;	
	}
	
	public boolean paramIsFloat(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null)return false;
		if(StringHelper.isFloat(v.toString().trim())){
			return true;
		}
		return false;
	}
	
	public boolean paramIsInteger(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null)return false;
		if(StringHelper.isInteger((v.toString().trim()))){
			return true;
		}
		return false;
	}
	
	public boolean paramIsLong(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null)return false;
		if(StringHelper.isLong((v.toString().trim()))){
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * request.getParameter("parameter_name")
	 * 
	 * if result is null , will return default value ""
	 * 
	 * */
	public String getAndSetStr(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key, "");
			return "";
		}
		else {
			String result=v.toString().trim();
			updateParameter(key,result);
			return result;
		}
	}
	
	public String getAndSetStr(String key,String defv){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key,defv);
			return defv;
		}
		else {
			String result=v.toString().trim();
			updateParameter(key,result);
			return result;
		}
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 同时会将返回结果的boolean类型重新set到request parameters中去
	 * 
	 * */
	public boolean getAndSetBool(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null)return false;
		boolean vr=false;
		try{
			vr=Boolean.parseBoolean(v.toString().trim());
			updateParameter(key,vr);
		}catch(Exception e){
			return false;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 同时会将返回结果的boolean类型重新set到request parameters中去
	 * 
	 * */	 
	public boolean getAndSetBool(String key,boolean _default){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key,_default);
			return _default;
		}
		boolean vr=false;
		try {
			vr= Boolean.parseBoolean(v.toString().trim());
			updateParameter(key,vr);
		}catch(Exception e){
			return _default;
		}
		return vr;
	}
	


	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 同时会将返回结果的long类型重新set到request parameters中去
	 * 
	 * */
	public long getAndSetLong(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key,0);
			return 0;
		}
		long vr=0;
		try{
			vr=Long.parseLong(v.toString());
			updateParameter(key,vr);
		}catch(Exception e){
			e.printStackTrace();
			return 0;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 同时会将返回结果的long类型重新set到request parameters中去
	 * 
	 * */
	public long getAndSetLong(String key,long _default){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key,_default);
			return _default;
		}
		long vr=0;
		try{
			vr=Long.parseLong(v.toString());
			updateParameter(key,vr);
		}catch(Exception e){
			return _default;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 同时会将返回结果的double类型重新set到request parameters中去
	 * 
	 * */
	public double getAndSetDouble(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key,0);
			return 0;
		}
		double vr=0;
		try{
			vr=Double.parseDouble(v.toString());
			updateParameter(key,vr);
		}catch(Exception e){
			return 0;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 同时会将返回结果的double类型重新set到request parameters中去
	 * 
	 * */
	public double getAndSetDouble(String key,double _default){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key,_default);
			return _default;
		}
		double vr=0;
		try{
			vr=Double.parseDouble(v.toString());
			updateParameter(key,vr);
		}catch(Exception e){
			return _default;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回默认值 0
	 * 同时会将返回结果的float类型重新set到request parameters中去
	 * 
	 * */
	public float getAndSetFloat(String key){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key,0);
			return 0;
		}
		float vr=0;
		try{
			vr=Float.parseFloat(v.toString());
			updateParameter(key,vr);
		}catch(Exception e){
			return 0;
		}
		return vr;
	}
	
	/**
	 * 
	 * @param key
	 * 
	 * @return
	 * 本方法对 request.getParameter("parameter_name")进行经典封装
	 * 如果结果为null，将会返回设置好的默认值
	 * 同时会将返回结果的float类型重新set到request parameters中去
	 * 
	 * */
	public float getAndSetFloat(String key,float _default){
		Object v=request.getParameter(key);
		if(v==null&&modified_params_map!=null){
			v=modified_params_map.get(key);
		}
		if(v==null){
			updateParameter(key,_default);
			return _default;
		}
		float vr=0;
		try{
			vr=Float.parseFloat(v.toString());
			updateParameter(key,vr);
		}catch(Exception e){
			return _default;
		}
		return vr;
	}
	
	/**
	 * 
	 * request.getParameterValues(key);
	 * return muti-value with a key
	 * 
	 * */
	public Map getParams(){
		if(modified_params_map!=null)return modified_params_map;
		return request.getParameterMap();
	}
	
	
	public String getSessionID(){
		return request.getSession().getId();
	}
	
	public HttpSession session(){
		return request.getSession();
	}
	
	public void setHeader(String key,String value){
		response.setHeader(key, value);
	}
	
	public void setDateHeader(String key,long value){
		response.setDateHeader(key, value);
	}
	
	public String getUserAgent(){
		return request.getHeader("user-agent");
	}
	
	public String getReferer(){
		return request.getHeader("Referer");
	}
	
	
	/**
	 * 
	 * 方法使用场景:当前端传的编码和后端不一致，导致乱码
	 * 
	 * @param
	 * 
	 * @return
	 * 
	 * 如果前端一个比较特殊的接口，传递的是gb2312编码，但是服务器使用的是utf-8
	 * 此时强行获取参数会得到乱码
	 * 这时本方法能将该指定的乱码，转为UTF-8格式编码
	 * 注意第一次表单参数获取，调用此方法才可避免乱码
	 * 
	 * 例子:
	 *    Event{
	 *    
	 *    	    handle(EventLoader el){
	 *    			
	 *    			String value=el.param("name","_default_value","gb2312",true);
	 *    			.
	 *    			.
	 *    			.
	 *    		}
	 *    		.
	 *    		.
	 *    		.
	 *    		.
	 *    
	 *    }
	 * 
	 * 
	 * */
	public String param(String key, String _default,String encode_type){
		String value=null;
		try{
			request.setCharacterEncoding("ISO-8859-1");
			value= new String(request.getParameter(key).getBytes("ISO-8859-1"),encode_type);
		}catch(Exception e){
			return _default;
		}
		if(StringHelper.isNull(value))value=_default;
		return value;
	}
	
}
